package cn.opencodes.framework.tools.vo;

import java.util.HashMap;
import java.util.Map;

/**
 * 返回数据
 * @author HJ
 */
public class R {
	private Object data;
	private int code;
	private String msg;
	
	@SuppressWarnings("unchecked")
	public R put(String key, Object value) {
		if (data == null) {
			data = new HashMap<>();
		}
		((Map<String, Object>)data).put(key, value);
		return this;
	}
	
	public R put(Object value) {
		data = value;
		return this;
	}
	
	public static R ok(String msg) {
		R r = new R();
		r.msg = msg;
		r.code = CoreConst.HttpStatus.OK.value();
		return r;
	}
	
	public static R ok() {
		return R.ok("OK");
	}
	
	public static R failure(String msg) {
		R r = new R();
		r.code = CoreConst.HttpStatus.FAILURE.value();
		r.msg = msg;
		return r;
	}
	
	public static R failure() {
		return R.failure("failure");
	}
	
	public static R error(int code, String msg) {
		R r = new R();
		r.code = code;
		r.msg = msg;
		return r;
	}
	
	public static R error(String msg) {
		return R.error(CoreConst.HttpStatus.ERROR.value(), msg);
	}
	
	public static R error() {
		return R.error("error");
	}


	public int getCode() {
		return code;
	}

	public String getMsg() {
		return msg;
	}


	public void setCode(int code) {
		this.code = code;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public Object getData() {
		return data;
	}

	public void setData(Object data) {
		this.data = data;
	}
}
